package cn.ssm.controller;

import cn.ssm.bean.Category;
import cn.ssm.service.impl.CategoryServiceImpl;
import cn.ssm.util.PageSite;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;

@RequestMapping("")  //用于加入到url中的多层路径
@Controller
public class CategoryController {

    @Autowired
    CategoryServiceImpl categoryService;

    @RequestMapping(value = "categories", method = RequestMethod.GET)
    public ModelAndView listCategory1(PageSite pageSite) {
        ModelAndView mav = new ModelAndView("listCategory");

        int pagelimit = 3;
        int totalcount = categoryService.total();
        pageSite.setCount(pagelimit);
        pageSite.calculateLast(totalcount);
        pageSite.calculateNum(totalcount);

        List<Category> lists = categoryService.list1(pageSite);
        Integer[] arr = new Integer[pageSite.getNumber()];

        mav.addObject("yeshu", Arrays.asList(arr)); //方便跳转页的foreach的遍历获取页码
        mav.addObject("listCategorys", lists);
        return mav;
    }

    @RequestMapping("list") //用PageHelper的分页
    public ModelAndView listCategory(PageSite pageSite) {
        pageSite.setCount(3);
        ModelAndView mav = new ModelAndView();
        PageHelper.offsetPage(pageSite.getStart(), 3);
        List<Category> lists = categoryService.list();
        int totalcount = (int) new PageInfo<>(lists).getTotal();

        pageSite.calculateLast(totalcount);
        pageSite.calculateNum(totalcount);

        mav.addObject("yeshu", Arrays.asList(new int[totalcount]));
        mav.addObject("listCategorys", lists);
        mav.setViewName("listCategory");
        return mav;
    }
    @RequestMapping(value = "categories", method = RequestMethod.POST)
    public ModelAndView insertCategory(Category category) {
        ModelAndView mav = new ModelAndView("redirect:/categories");
        categoryService.insert(category);
        return mav;
    }
    @RequestMapping(value = "categories/{id}", method = RequestMethod.DELETE)
    public ModelAndView deleteCategory(Category category) {
        ModelAndView mav = new ModelAndView("redirect:/categories");
        categoryService.delete(category.getId());
        return mav;
    }
    @RequestMapping(value = "categories/{id}", method = RequestMethod.GET)
    public ModelAndView editCategory(Category category) {
        ModelAndView mav = new ModelAndView("editCategory");
        Category c = categoryService.get(category.getId());
        mav.addObject("category", c);
        return mav;
    }

    @RequestMapping(value = "categories/{id}", method = RequestMethod.PUT)
    public ModelAndView updateCategory(Category category) {
        ModelAndView mav = new ModelAndView("redirect:/categories");
        categoryService.update(category);
        return mav;
    }
}
